
import { registerClass } from "../../../cfw/tools/class_decorator";
import { SDKState, ADName } from "../../SDKDefine";
import SDKHelper from "../../tools/SDKHelper";
import BaseAd from "../../core/base/BaseAd";
// import SDKEventID from "../../third/SDKEventID";
// Learn TypeScript:
//  - https://docs.cocos.com/creator/manual/en/scripting/typescript.html
// Learn Attribute:
//  - https://docs.cocos.com/creator/manual/en/scripting/reference/attributes.html
// Learn life-cycle callbacks:
//  - https://docs.cocos.com/creator/manual/en/scripting/life-cycle-callbacks.html


@registerClass('WXCustomAd')
export default class WXCustomAd extends BaseAd {



    open(): void {
        this.destroy()
        this.create();
        this.show()
    }

    updateSize() {
        if (this.ad) {
            // let winSize = this.sdk.getSystemInfoSync();
            let left = this.style.left
            let top = this.style.top
            this.ad.style.left = left;
            this.ad.style.top = top;
        }
    }

    // getStyle() {

    //     // let winSize = this.sdk.getSystemInfoSync();
    //     let left = this.style.left
    //     let top = this.style.top
    //     // let left = 0
    //     // let top = 0;
    //     return {
    //         left: left,
    //         top: top,
    //     }
    // }

    create() {
        this.setState(SDKState.loading)
        if (!this.ad) {
            this.ad = this.sdk.createCustomAd({
                adUnitId: this.adUnitID,
                adIntervals: 30,
                style: this.getStyle(),
            })
            this.ad.onLoad(this.getFunc(this.onLoad));
            this.ad.onError(this.getFunc(this.onError));
            // this.ad.onHide(this.getFunc(this.onHide));
            // this.ad.onClose(this.getFunc(this.onClose));
        }
    }

    show() {
        this.setState(SDKState.show)
        if (this.ad) {
            this.ad.show();
        }
    }

    onClose() {

    }



    onHide() {

    }


    close() {
        this.setState(SDKState.close)

        if (SDKHelper.isFunction(this.ad, 'hide')) {

            this.ad.hide();
        }
    }

    destroy() {
        if (this.ad) {
            this.ad.offLoad(this.getFunc(this.onLoad))
            this.ad.offError(this.getFunc(this.onError))
            // this.ad.offHide(this.getFunc(this.onHide))
            // this.ad.offClose(this.getFunc(this.onClose))
            this.ad.destroy();
            this.ad = null;
        }
    }
}
